package zhoufu.sz.sanxiao_check.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.FragmentModifyActionBinding;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.CheckItemModel;
import zhoufu.sz.sanxiao_check.navigation.Navigator;
import zhoufu.sz.sanxiao_check.presents.ModifyActionPresenter;
import zhoufu.sz.sanxiao_check.ui.contract.ModifyActionContract;
import zhoufu.sz.sanxiao_check.utils.AbDateUtil;

/**
 * 项目 ： XTSanXiao_V2
 * 开发 ： hyc
 * 时间 ： 16-8-5 上午9:58
 * 功能 ： 需要责任人和检查人员签名;
 */
public class ModifyActionFragment extends BaseFragment
        implements ModifyActionContract.View, DatePickerFragment.TimeProxyListener
{

    @Inject
    ModifyActionPresenter _actionPresenter;
    @Inject
    Navigator             _navigator;
    private final int PIC_SHOW_SIGN = 0x10;
    private final int PIC_SHOW_GES  = 0x13;
    private final int PIC_LEFT      = 0x11;
    private final int PIC_RIGHT     = 0x12;

    private static String CHECKITEMMODEL = "CHECKITEMMODEL";
    private FragmentModifyActionBinding _dataBinding;
    private CheckItemModel              _itemModel;

    public static ModifyActionFragment newInstance(CheckItemModel model)
    {
        Bundle args = new Bundle();
        args.putSerializable(CHECKITEMMODEL, model);
        ModifyActionFragment fragment = new ModifyActionFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState)
    {
        LoginComponet component = getComponent(LoginComponet.class);
        component.inject(this);
        _dataBinding = DataBindingUtil
                .inflate(inflater, R.layout.fragment_modify_action, container, false, component);
        setHasOptionsMenu(true);
        return _dataBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        _itemModel = (CheckItemModel) getArguments().getSerializable(CHECKITEMMODEL);
        if ( TextUtils.isEmpty(_itemModel.getAddTime()) )
        {
            _itemModel.setAddTime(AbDateUtil.getCurrentDate(AbDateUtil.dateFormatYMDHMS));
        }
        _itemModel.setComplete_Time(AbDateUtil.getCurrentDateByOffset(AbDateUtil.dateFormatYMDHMS,
                Calendar.MONTH, 1));//整改时间默认当前时间后的一个月
        _dataBinding.setModel(_itemModel);
        _actionPresenter.bindV(this);
        getmAct().setTitle(getString(R.string.title_print));
        _dataBinding.tvLimitTime.setOnClickListener(this);
        _dataBinding.tvTakeSign.setOnClickListener(this);
        _dataBinding.imgLeftSign.setOnClickListener(this);
        _dataBinding.imgRightSign.setOnClickListener(this);
        _dataBinding.imgShowSign.setOnClickListener(this);
    }


    @Override
    public void onClick(View v)
    {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.tv_limit_time:
                // 选择时间
                DatePickerFragment timePickerFragment = new DatePickerFragment();
                timePickerFragment.show(getChildFragmentManager(), "timePicker");
                timePickerFragment.setTimeProxyListener(this);
                break;
            case R.id.tv_take_sign:
                //选择图片
                //_navigator.selectImage(this, PIC_SHOW_SIGN);
                _navigator.selectMaxOneImage(mResultCallback);
                break;
            case R.id.img_left_sign:
                _navigator.showGesture(this, PIC_LEFT);

                break;
            case R.id.img_right_sign:
                _navigator.showGesture(this, PIC_RIGHT);
                break;
            //展示,签名
            case R.id.img_show_sign:
                _navigator.showGesture(this, PIC_SHOW_GES);
                break;
        }
    }

    private GalleryFinal.OnHanlderResultCallback mResultCallback
            = new GalleryFinal.OnHanlderResultCallback()
    {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList)
        {
            if ( resultList != null && resultList.size() > 0 )
            {
                String url = resultList.get(0).getPhotoPath();
                _itemModel.setEvidence(url);
            }
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg)
        {
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if ( resultCode == Activity.RESULT_OK )
        {
            String url;
            switch (requestCode)
            {
                case PIC_SHOW_GES:
                    url = data.getStringExtra("data");
                    _itemModel.setEvidence(url);
                    break;
                case PIC_LEFT:
                    url = data.getStringExtra("data");
                    _itemModel.setRemark(url);
                    break;
                case PIC_RIGHT:
                    url = data.getStringExtra("data");
                    _itemModel.setRemark2(url);
                    break;
            }
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_save, menu);
        MenuItem item = menu.getItem(0);
        item.setTitle("保存");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        //  保存
        _actionPresenter.update(_itemModel);
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showToast(String msg)
    {

    }

    @Override
    public void pullFaild()
    {
        hideProgreesDialog();
        Toast.makeText(mAct, "上传失败,请重新保存", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override
    public void pullStart()
    {
        showProgreesDialog(false);
    }

    @Override
    public void updateSuccess()
    {
        // 跳转到展示巡查文书页面
        _navigator.startPrintAct(this.getContext(), _itemModel);
        getmAct().finish();
    }

    @Override
    public void onTime(int year, int month, int day)
    {
        System.out.println("year = " + year);
        System.out.println("month = " + month);
        month += 1;
        String date = year + "/" + month + "/" + day;

        _itemModel.setComplete_Time(date);
    }
}
